home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 126-150 / disk_130 / patedit / pateditsubs.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  245 lines

  1. /* subroutines to make pattern play program work */
  2.  
  3. #include "stdio.h"
  4. #include "fcntl.h"
  5.  
  6. extern struct Screen *sC;
  7. extern struct Window *wG;
  8. extern struct RastPort *rpG;
  9. extern int mousex, mousey;
  10.  
  11. static char    def_name[50] = "";
  12. static char    def_dir[50] = "";
  13.  
  14. struct patdata
  15. {
  16. unsigned int ourpattern[32], power_of_2, pen1color, pen2color;
  17. } it;
  18.  
  19. extern struct Gadget Pen1;
  20. DoPen1()
  21. {
  22.    it.pen1color= (mousex-Pen1.LeftEdge)/(Pen1.Width/4)+4*((mousey-
  23.        Pen1.TopEdge)>Pen1.Height/2);
  24.    fillsamples();
  25. }
  26.  
  27. extern struct Gadget Pen2;
  28. DoPen2()
  29. {
  30.    it.pen2color= (mousex-Pen2.LeftEdge)/(Pen2.Width/4)+4*((mousey-
  31.        Pen2.TopEdge)>Pen2.Height/2);
  32.    fillsamples();
  33. }
  34.  
  35. extern struct Gadget Pen1Sample, Pen2Sample, Bigsample;
  36.  
  37. fillsamples()
  38. {
  39.    SetAPen(rpG, (long)it.pen1color);
  40.    
  41.    SetAfPt(rpG, 0l, 0l);
  42.    SetDrMd(rpG, JAM1);
  43.    RectFill(rpG, (long)Pen1Sample.LeftEdge, (long)Pen1Sample.TopEdge,
  44.        (long)(Pen1Sample.LeftEdge+Pen1Sample.Width),
  45.        (long)(Pen1Sample.TopEdge+Pen1Sample.Height));
  46.  
  47.    SetAPen(rpG, (long)it.pen2color);
  48.    RectFill(rpG, (long)Pen2Sample.LeftEdge, (long)Pen2Sample.TopEdge,
  49.        (long)(Pen2Sample.LeftEdge+Pen2Sample.Width),
  50.        (long)(Pen2Sample.TopEdge+Pen2Sample.Height));
  51.  
  52.    SetAPen(rpG, (long)it.pen1color);
  53.    SetBPen(rpG, (long)it.pen2color);
  54.    SetDrMd(rpG, JAM1|JAM2);
  55.    SetAfPt(rpG, it.ourpattern, (long)it.power_of_2);
  56.    RectFill(rpG, (long)Bigsample.LeftEdge, (long)Bigsample.TopEdge,
  57.        (long)(Bigsample.LeftEdge+Bigsample.Width),
  58.        (long)(Bigsample.TopEdge+Bigsample.Height-1));
  59.  
  60.    RefreshGadgets(&Pen1Sample, wG, 0l);
  61. }
  62.  
  63. extern struct Gadget Grid;
  64. DoGrid()
  65. {
  66. int bit, word, mask;
  67.  
  68.    bit = (mousex-Grid.LeftEdge)/(Grid.Width/16);
  69.    mask=0x8000>>bit;
  70.    word= (mousey-Grid.TopEdge)/(Grid.Height/32);
  71.    
  72.    if (word>=(1<<it.power_of_2))
  73.    {   DisplayBeep(0l);
  74.        return;
  75.    }
  76.    
  77.    it.ourpattern[word]^=mask;
  78.    
  79.    drawbit(bit, word, it.ourpattern[word]&mask);
  80.    
  81.    fillsamples();
  82. }
  83.  
  84. drawbit(bit, word, flag)
  85. int bit, word, flag;
  86. {
  87.  
  88.    SetAPen(rpG, flag? 0l: 1l);
  89.    SetAfPt(rpG, 0l, 0l);
  90.    SetDrMd(rpG, JAM1);
  91.    RectFill(rpG, (long)(Grid.LeftEdge+4*bit), (long)(Grid.TopEdge+4*word),
  92.                (long)(Grid.LeftEdge+4*bit+2), (long)(Grid.TopEdge+4*word+2));
  93. }
  94.  
  95. extern struct PropInfo SizerSInfo;
  96. SetSize()
  97. {
  98. unsigned int x, y, size, mask;
  99.  
  100.    x=SizerSInfo.VertPot>>11;
  101.    for(size=1, it.power_of_2=0; x; x=x>>1, size*=2, ++it.power_of_2);
  102.    
  103.    SetAPen(rpG, 3l);
  104.    SetAfPt(rpG, 0l, 0l);
  105.    SetDrMd(rpG, JAM1);
  106.    RectFill(rpG, (long)(Grid.LeftEdge), (long)(Grid.TopEdge),
  107.                (long)(Grid.LeftEdge+Grid.Width-1),
  108.                (long)(Grid.TopEdge+Grid.Height-1));
  109.  
  110.    for (x=0, mask=0x8000; x<16; ++x, mask=mask>>1)
  111.    {   for (y=0; y<size; ++y)
  112.            drawbit(x, y, mask&it.ourpattern[y]);
  113.    }
  114.    
  115.    fillsamples();
  116. }
  117.  
  118. Clearit()
  119. {
  120. int i;
  121.    for (i=0; i<32; ++i) it.ourpattern[i]=0;
  122.    SetSize();
  123. }
  124.  
  125. char *index();
  126.  
  127. call_get_fname(filename, greet)
  128. char filename[], *greet;
  129. {
  130. char *p;
  131. int t;
  132.  
  133.    if (get_fname(wG,sC,greet,def_name,def_dir))
  134.    {
  135.        if ((p=index(def_name, '.'))) *p=0;
  136.  
  137.        if (def_dir[0])
  138.        {   strcpy(filename, def_dir);
  139.            if ((t=filename[strlen(filename)-1]!=':') && t!='/')
  140.                            strcat(filename, "/");
  141.        }
  142.        else filename[0]=0;
  143.        strcat(filename,def_name);
  144.        strcat(filename,".pat");
  145.        
  146.        return 1;
  147.    }
  148.    return 0;
  149. }
  150.  
  151. Loadit()
  152. {
  153. char filename[70];
  154. int fileno;
  155.  
  156.    if (call_get_fname(filename, "Load"))
  157.    {
  158.        
  159.        if ((fileno=open(filename, O_RDONLY))==-1)
  160.        {
  161. badexit:   if (fileno!=-1) close(fileno);
  162.            DisplayBeep(sC);
  163.            return -1;
  164.        }
  165.        if (read(fileno, &it, sizeof(struct patdata))==0) goto badexit;
  166.        close(fileno);
  167.    
  168.        SizerSInfo.VertPot=((1<<it.power_of_2)-1)<<11;
  169.    }
  170.    SetSize();
  171. }
  172.  
  173.  
  174. Saveit()
  175. {
  176. char filename[70], *p;
  177. int fileno, t;
  178. FILE *file;
  179.  
  180.    if (call_get_fname(filename, "Save"))
  181.    {
  182.  
  183.        if ((fileno=open(filename, O_RDWR|O_CREAT))==-1)
  184.        {
  185. badexit:   if (fileno!=-1) close(fileno);
  186.            DisplayBeep(sC);
  187.            return -1;
  188.        }
  189.        if (write(fileno, &it, sizeof(struct patdata))==-1) goto badexit;
  190.        close(fileno);
  191.  
  192.        p=index(filename,'.'); *p=0;
  193.        strcat(filename,".h");
  194.        if (!(file=fopen(filename, "w"))) goto badexit;
  195.    
  196.        fprintf(file, "char %s[]=\n{   ", def_name);
  197.        for (t=0; t<((1<<it.power_of_2)-1); ++t)
  198.            fprintf(file, "0x%04.4x,\n    ", it.ourpattern[t]);
  199.        fprintf(file, "0x%04.4x\n};\n", it.ourpattern[t]);
  200.    
  201.        fprintf(file, "#define %s_size %dl\n", def_name, it.power_of_2);
  202.    
  203.        fclose(file);
  204.    }
  205.    SetSize();
  206. }
  207.  
  208. init()
  209. {
  210. int swatchwidth, swatchheight, x;
  211.  
  212.    swatchwidth=Pen1.Width/4; swatchheight=Pen1.Height/2;
  213.    SetAfPt(rpG, 0l,0l);
  214.    SetDrMd(rpG, JAM1);
  215.    for (x=0; x<4; ++x)
  216.    {   SetAPen(rpG, (long)x);
  217.        RectFill(rpG, (long)(Pen1.LeftEdge+x*swatchwidth), (long)Pen1.TopEdge,
  218.         (long)(Pen1.LeftEdge+(x+1)*swatchwidth-1),
  219.         (long)(Pen1.TopEdge+swatchheight-1));
  220.        RectFill(rpG, (long)(Pen2.LeftEdge+x*swatchwidth), (long)Pen2.TopEdge,
  221.         (long)(Pen2.LeftEdge+(x+1)*swatchwidth-1),
  222.         (long)(Pen2.TopEdge+swatchheight-1));
  223.        SetAPen(rpG, (long)(x+4));
  224.        RectFill(rpG, (long)(Pen1.LeftEdge+x*swatchwidth),
  225.         (long)(Pen1.TopEdge+swatchheight),
  226.         (long)(Pen1.LeftEdge+(x+1)*swatchwidth-1),
  227.         (long)(Pen1.TopEdge+Pen1.Height-1));
  228.        RectFill(rpG, (long)(Pen2.LeftEdge+x*swatchwidth),
  229.         (long)(Pen2.TopEdge+swatchheight),
  230.         (long)(Pen2.LeftEdge+(x+1)*swatchwidth-1),
  231.         (long)(Pen2.TopEdge+Pen2.Height-1));
  232.    }
  233.  
  234.    SizerSInfo.VertPot=0x7fff;
  235.    SetSize();
  236. }
  237.  
  238. extern struct Requester RequesterStructure2;
  239.  
  240. tellit()
  241. {
  242.    Request(&RequesterStructure2, wG);
  243. }
  244.  
  245.